import os

from mastodon import Mastodon

CLIENT_SECRET = 'diegor2test_clientcred.secret'
BASE_URL = 'https://masto.donte.com.br'
USER_SECRET_POSTFIX = '_usercred.secret'

class MastodonBot(object):

    def __init__(self, chatid):
        self.secret = str(chatid) + USER_SECRET_POSTFIX
        self.is_logged_in = os.path.isfile(self.secret)
        self.mastodon = Mastodon(
            client_id = CLIENT_SECRET,
            api_base_url = BASE_URL,
            access_token = self.secret
        )

    def get_oauth_url(self):
        return self.mastodon.auth_request_url()

    def login(self, chatid, token):
        try:
            token = self.mastodon.log_in(code = token, to_file = self.secret)
            self.mastodon.access_token = token
            self.is_logged_in = True
        except MastodonIllegalArgumentError:
            self.is_logged_in = True

    def submit(self, title, url):
        toot = self.mastodon.status_post('{}\n\n{}'.format(title, url))
        print(toot)

